package controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import model.DbConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.sun.corba.se.spi.activation.Server;
import model.Barang;
import model.Users;
import model.ModelAkun;
import model.ModelBarang;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;

public class LogicBarang {

    HttpServletRequest request;

    public LogicBarang(HttpServletRequest request) {
        this.request = request;
    }

    public String add() {
        String nama = request.getParameter("nama");
        String jenis = request.getParameter("jenis");
        String harga = request.getParameter("harga");
        String dis = request.getParameter("dis");

        DbConnection dbConn = new DbConnection();
        ModelBarang mdBar = new ModelBarang(dbConn.getConnection());

        try {
            mdBar.add(nama, jenis, harga, dis);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        //redirect method
        return listBarang();
    }

    //select one
    public String selectOnexxx() {
        //String idBarang = request.getParameter("idBarang");
        String idBarang = request.getParameter("idBarang");
        List<Barang> barang = new ArrayList<Barang>();
        DbConnection dbConn = new DbConnection();
        ModelBarang mdHap = new ModelBarang(dbConn.getConnection());
        try {

            int id = Integer.parseInt(idBarang);
            barang = mdHap.selectone(id);
            request.setAttribute("barangs", barang);
            //request.setAttribute(null, this);


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return "barang.jsp";
    }

    //one
    public String selectone() {
        String idBarang = request.getParameter("idBarang");
        List<Barang> barang = new ArrayList<Barang>();
        DbConnection dbConn = new DbConnection();
        ModelBarang mdHap = new ModelBarang(dbConn.getConnection());
        try {

            int id = Integer.parseInt(idBarang);
            barang = mdHap.selectone(id);
            request.setAttribute("barangs", barang);
            //request.setAttribute(null, this);


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return "barang_update.jsp";
    }

    public String update() {

        String idBarang = request.getParameter("idBarang");
        String nama = request.getParameter("nama");
        String jenis = request.getParameter("jenis");
        String harga = request.getParameter("harga");
        String dis = request.getParameter("dis");

        DbConnection dbConn = new DbConnection();
        ModelBarang mdBar = new ModelBarang(dbConn.getConnection());


        try {
            int id = Integer.parseInt(idBarang);
            mdBar.update(id, nama, jenis, harga, dis);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return listBarang();
    }

    public String goProfil() throws Exception {
        HttpSession session = request.getSession();
        Users user = new Users();
        DbConnection dbConn = new DbConnection();
        ModelAkun mdLogin = new ModelAkun(dbConn.getConnection());
        if (session.getAttribute("uname") != null) {
            String username = (String) session.getAttribute("uname");
            user = mdLogin.getUser(username);
            if (user.getAdminStat() == 1) {
                request.setAttribute("user", user);
                dbConn.closeConnection();
                return "profilAdmin.jsp";
            } else {
                request.setAttribute("user", user);
                dbConn.closeConnection();
                return "profil.jsp";
            }
        } else {
            return "login.jsp";
        }
    }

    public String goEditProfil() {
        String username = request.getParameter("username");
        Users user = new Users();
        DbConnection dbConn = new DbConnection();
        ModelAkun mdEdit = new ModelAkun(dbConn.getConnection());
        try {
            user = mdEdit.getUser(username);
            request.setAttribute("user", user);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return "editProfil.jsp";
    }

    public String Userupdate() throws Exception {
        HttpSession session = request.getSession();
        String nama = request.getParameter("nama");
        String alamat = request.getParameter("alamat");
        String telepon = request.getParameter("telepon");
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        Users user = new Users();
        DbConnection dbConn = new DbConnection();
        ModelAkun mdEdit = new ModelAkun(dbConn.getConnection());

        if (session.getAttribute("uname") != null) {
            String username = (String) session.getAttribute("uname");
            mdEdit.updateUser(nama, alamat, telepon, email, password, username);
            user = mdEdit.getUser(username);
            if (user.getAdminStat() == 1) {
                request.setAttribute("user", user);
                dbConn.closeConnection();
                return "profilAdmin.jsp";
            } else {
                request.setAttribute("user", user);
                dbConn.closeConnection();
                return "profil.jsp";
            }
        } else {
            return "login.jsp";
        }

    }

    //list barang Bos
    public String listBarang() {
        //String idBarang = request.getParameter("idBarang");
        List<Barang> barang = new ArrayList<Barang>();
        DbConnection dbConn = new DbConnection();
        ModelBarang mdHap = new ModelBarang(dbConn.getConnection());
        try {

            barang = mdHap.getAllBarang();
            request.setAttribute("barangs", barang);
            //request.setAttribute(null, this);


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return "barang.jsp";
    }

    //delete barang
    public String delete() {
        String idBarang = request.getParameter("idBarang");
        //List<Barang> barang = new ArrayList<Barang>();
        DbConnection dbConn = new DbConnection();
        ModelBarang mdHap = new ModelBarang(dbConn.getConnection());
        try {

            //barang = mdHap.getAllBarang();
            //request.setAttribute("barangs", barang);
            //request.setAttribute(null, this);
            int id = Integer.parseInt(idBarang);
            mdHap.delete(id);
            //user = mdHap.getAllUsers();
            //request.setAttribute("barangs", barang);


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return listBarang();
    }

    public String uploadadd() {
        String[] array = new String[5];
        File savedFile = null;
        boolean isMultipart = FileUpload.isMultipartContent(request);
        if (!isMultipart) {
            System.out.println(" Form Bukan Multipart...!!!");
            return "";
        }
        DiskFileUpload upload = new DiskFileUpload();
        List items = null;
        try {
            items = upload.parseRequest(request);
        } catch (Exception e) {
            System.out.println("Exception = " + e.getMessage());
        }

        Iterator itr = items.iterator();
        int count = 0;
        while (itr.hasNext()) {
            FileItem item = (FileItem) itr.next();
            if (item.isFormField()) {
                //menginput data pada field yang input type text
                array[count] = item.getString();
                count++;

            } else {
                File fullFile = new File(item.getName());
                File nama = new File(item.getFieldName());
                System.out.println(nama);
                System.out.println("File Lengkap : " + fullFile);
                String sPath = request.getServletContext().getRealPath("foto");
                System.out.println("Lokasi penyimpanan file =" + sPath);
                savedFile = new File(sPath, fullFile.getName());
                //nama filenya....!!!!
                DbConnection dbConn = new DbConnection();
                ModelBarang mdBar = new ModelBarang(dbConn.getConnection());
                Barang brg = new Barang();
                try {
                    item.write(savedFile);
                    brg.setNamaBarang(array[0]);
                    brg.setJenisBarang(array[1]);
                    brg.setHargaBarang(Integer.parseInt(array[2]));
                    brg.setDiskripsiBarang(array[3]);
                    brg.setFoto(fullFile.getName());
                    mdBar.saveupload(brg);
                    return listBarang();
                } catch (Exception e) {
                    return listBarang();
                }
            }
        }
        return listBarang();
    }
}
